package com.itzmn.java.javase.designpattern.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: 张梦楠
 * @Date: 2018/12/1 15:36
 * 简书：https://www.jianshu.com/u/d611be10d1a6
 * 码云：https://gitee.com/zhangqiye
 * @Description:
 */
public class MeiPo implements MethodInterceptor {

    /**
     * 获取被代理的对象
     * @param c
     * @return
     */
    public Object getInstace(Class c){

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(c);
        enhancer.setCallback(this);
        return enhancer.create();

    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("代理增强");
        // 一定要执行父类，这个传入的对象是代理类，
        methodProxy.invokeSuper(o,objects);
        return null;
    }
}
